package com.example.readandwriteseparation.rest;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.thread.ThreadUtil;
import com.example.readandwriteseparation.enitiy.Orders;
import com.example.readandwriteseparation.service.IOrdersService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 测试
 *
 * @author qingyan 2021-09-06
 */
@RestController
public class OrderController {

    @Autowired
    private IOrdersService ordersService;

    @GetMapping("test1")
    public String test1() {
        Orders orders = new Orders()
                .setSupplierId(1)
                .setSupplierOrderCode("1")
                .setOrderCode(UUID.fastUUID().toString())
                .setMenuId(1)
                .setCandidateId(1)
                .setSubmitDateTime(LocalDateTime.now())
                .setSubmitUserId(1)
                .setCostDays(10)
                .setAuthorizeType(0)
                .setTotalMoney(BigDecimal.ZERO)
                .setCreateTime(LocalDateTime.now())
                .setUpdateTime(LocalDateTime.now());
        ordersService.insert(orders);
        return "success";
    }

    @GetMapping("test2")
    public List<Orders> test2() {
        return ordersService.list();
    }

    @GetMapping("test4")
    public List<Orders> test4() {
        return ordersService.listByMaster();
    }

    @GetMapping("test3")
    public String test3() {
        ExecutorService executorService = ThreadUtil.newExecutor(10, 10, Integer.MAX_VALUE);
        List<List<Orders>> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            List<Orders> orders = new ArrayList<>();
            for (int j = 0; j < 1000; j++) {
                Orders order = new Orders()
                        .setSupplierId(1)
                        .setSupplierOrderCode("1")
                        .setOrderCode(UUID.fastUUID().toString())
                        .setMenuId(1)
                        .setCandidateId(1)
                        .setSubmitDateTime(LocalDateTime.now())
                        .setSubmitUserId(1)
                        .setCostDays(10)
                        .setAuthorizeType(0)
                        .setTotalMoney(BigDecimal.ZERO)
                        .setCreateTime(LocalDateTime.now())
                        .setUpdateTime(LocalDateTime.now());
                orders.add(order);
            }
            list.add(orders);
        }
        executorService.execute(()->{
            list.parallelStream().forEach(ordersService::saveBatch);
        });
        return "success";
    }
    @GetMapping("test5")
    public String test5(){
        ordersService.switchDb();
        return "success";
    }


}
